<?php

namespace app\common\model;

use think\Model;

class FormworkVersion extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 定义字段类型
    protected $type = [
    ];
    protected $append = [
        'template'
    ];

    /**
     * 所属模板
     */
    public function getTemplateAttr($value, $data)
    {
        if($tpl = Formwork::get($data['formwork_id'])) {
           return $tpl['title'];
        }
        return '--';
    }
    
    /**
     * 关联模板表
     */
    public function formwork()
    {
        return $this->belongsTo('app\\common\\model\\Formwork','formwork_id','id');
    }

    /**
     * 根据模板编号获取最新版本
     */
    public static function getNews($formwork_id)
    {
        $where['formwork_id'] = $formwork_id;
        $where['show'] = 1;
        return self::where($where)->order('id','desc')->find();
    }

    /**
     * 获取详情
     */
    public static function getDraftId($draft_id)
    {
        return self::where('draft_id',$draft_id)->find();
    }
}
